import request from "../../plugins/request"
import { getShowNginxImage, getShowNginxPageConfig } from "../../../config"
import store from "../../utils/store"

Page({
	data: {
		bannerList: [],
		appList: [],
		articleList: [],
	},
	onLoad: async function (options) {
		const obj = await request({
			url: "api/wx/get-stu-notice",
			noAuth: true,
			data: {
				payloads: {
					schoolId: store.schoolId.get(),
				},
			},
			showLoading: true,
			errorHandle: true,
		})

		// 从服务器下载 pageConfig 文件并反序列化为 js 对象
		// const data = await request({
		// 	url: getShowNginxPageConfig("xiaoWuXingMiniProgram", "newUserInfo.json"),
		// 	method: "GET",
		// 	noAuth: true,
		// 	row: true,
		// })
		//轮播图图片列表
		const bannerList = obj.bannerList.map((item) => {
			return {
				...item,
				backgroundImage: this.prependBase64Prefix(item.backgroundImage),
			}
		})
		//应用列表
		const appList = obj.appList.map((item) => {
			return {
				...item,
				backgroundImage: this.prependBase64Prefix(item.backgroundImage),
			}
		})
		// 文章列表
		const articleList = obj.articleList.map((item) => {
			return {
				...item,
				contentList: item.contentList.map((content) => {
					// 处理文章图标，如果有图标则显示，否则显示默认的图标
					let icon
					if (content.icon) {
						icon = this.prependBase64Prefix(content.icon)
					} else {
						icon = getShowNginxImage("eQtGfV6ED2bDkZqhqpXs.png")
					}
					return {
						...content,
						icon,
					}
				}),
			}
		})
		this.setData({
			bannerList,
			appList,
			articleList,
		})
	},
	//处理页面跳转
	handleJumpPage(e) {
		const item = e.currentTarget.dataset.item
		console.log(item, "item")
		const { jumpType, jumpPath, appId } = item
		switch (jumpType) {
			// 跳转到小程序
			case 3:
				wx.navigateToMiniProgram({
					appId,
					path: jumpPath,
				})
				break
			// 跳转到页面
			case 2:
				wx.navigateTo({
					url: jumpPath,
				})
				break
			// 跳转到 webview
			case 4:
				wx.navigateTo({
					url: `/pages/web/web?path=${jumpPath}`,
				})
				break
			default:
				break
		}
	},
	// 显示联系客服弹窗
	showContractKefuPopup() {
		const contractKefuPopup = this.selectComponent("#contractKefuPopup")
		contractKefuPopup.open()
	},
	// 传入 row base64，拼接上前缀，使图片能够正常渲染
	prependBase64Prefix(rowBase64) {
		const prefix = "data:image/png;base64,";
		return prefix + rowBase64;
	}
})
